package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.local.SellCarDB;
import com.example.gzysmartcity.repository.model.Commodity;
import com.example.gzysmartcity.repository.model.CommodityType;
import com.example.gzysmartcity.repository.model.SellCar;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class SellViewModel extends ViewModel {
    private final MutableLiveData<State> sellLiveData = new MutableLiveData<>();
    private List<Commodity.DataBean> commodityList;
    private List<CommodityType.DataBean> commodityTypeList;
    private int type;
    private List<Object> brImgList;

    public void insertSellCar(SellCar sellCar) {
        SellCar byId = SellCarDB.getById(sellCar.getId());
        if (byId != null) {
            byId.setNum(sellCar.getNum() + byId.getNum());
            SellCarDB.update(byId);
        } else {
            SellCarDB.inserts(sellCar);
        }
    }

    public void getCommodityData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Commodity>() {
            @Override
            public Commodity doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Commodity/getAll?", null, Commodity.class, 0);
            }

            @Override
            public void onSuccess(Commodity result) {
                commodityList = result.getData();
                getCommodityTypeData();
            }

            @Override
            public void onFail(Throwable t) {
                sellLiveData.postValue(State.FAIL);
            }
        });
    }

    public void getCommodityTypeData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<CommodityType>() {
            @Override
            public CommodityType doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("CommodityType/getAll?", null, CommodityType.class, 0);
            }

            @Override
            public void onSuccess(CommodityType result) {
                commodityTypeList = result.getData();

                getCommodityAllData();
            }


            @Override
            public void onFail(Throwable t) {
                sellLiveData.postValue(State.FAIL);

            }
        });
    }

    public void getTypeId(int id) {
        this.type = id;
    }

    public void getCommodityAllData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() {
                brImgList = new ArrayList<>();
                for (Commodity.DataBean dataBean : commodityList.subList(0, 5)) {
                    brImgList.add(dataBean.getPic());
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                sellLiveData.postValue(State.SUCCESS);
            }
        });
    }

    public void getCommoditySortMoney(List<Commodity.DataBean> list, Boolean flag) {
        if (flag) {
            list.sort(Comparator.comparing(Commodity.DataBean::getCostPrice).reversed());
        } else {
            list.sort(Comparator.comparing(Commodity.DataBean::getCostPrice));
        }
    }

    public void getCommoditySortVolume(List<Commodity.DataBean> list, Boolean flag) {
        if (flag) {
            list.sort(Comparator.comparing(Commodity.DataBean::getVolume).reversed());
        } else {
            list.sort(Comparator.comparing(Commodity.DataBean::getVolume));
        }
    }

    public void getCommodityDiscount(List<Commodity.DataBean> list, Boolean flag) {
        for (Commodity.DataBean d : list) {
            d.setMoneyDis(Double.parseDouble(d.getMoney()) - Double.parseDouble(d.getCostPrice()));
        }
        if (flag) {
            list.sort(Comparator.comparing(Commodity.DataBean::getMoneyDis).reversed());
        } else {
            list.sort(Comparator.comparing(Commodity.DataBean::getMoneyDis));
        }
    }

    public void getCommodityRvData(List<Commodity.DataBean> list, int id) {
        list.clear();
        list.addAll(commodityList.stream().filter(a -> a.getCommodityTypeId() == id).collect(Collectors.toList()));
        type = id;
    }

    public void search(List<Commodity.DataBean> list, String name) {
        LogUtils.i(name);
        list.clear();
        if (name.equals(""))
            list.addAll(commodityList.stream().filter(a -> a.getCommodityTypeId() == type)
                    .collect(Collectors.toList()));
        else
            list.addAll(commodityList.stream().filter(a -> a.getCommodityTypeId() == type)
                    .filter(a -> a.getCommodityName().contains(name))
                    .collect(Collectors.toList()));
    }

    public MutableLiveData<State> getSellLiveData() {
        return sellLiveData;
    }

    public List<CommodityType.DataBean> getCommodityTypeList() {
        return commodityTypeList;
    }

    public List<Object> getBrImgList() {
        return brImgList;
    }
}
